package com.atung.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atung.dao.BusCarAllocationDAO;
import com.atung.dao.BusCarRideDAO;
import com.atung.dao.BusOrderCarDAO;
import com.atung.model.BusCarRide;
import com.atung.model.BusOrderCarDTO;
import com.atung.model.CarMessageDTO;
import com.atung.model.CompanionDTO;
import com.atung.response.ResponseBusOrderCarList;
import com.atung.response.ResponseOrderDetails;
import com.atung.service.BusOrderCarService;

import cn.hutool.core.bean.BeanUtil;

@Service
public class BusOrderCarServiceImpl implements BusOrderCarService{

	@Autowired
	private BusCarAllocationDAO busCarAllocationDAO;
	@Autowired
	private BusOrderCarDAO busOrderCarDAO;
	@Autowired
	private BusCarRideDAO busCarRideDAO;
	/**
	 * 订单列表
	 * @param openId
	 * @param orderState
	 * @return
	 */
	public List<ResponseBusOrderCarList>getOrderList(String openId, Integer orderState){
		
		
		return busOrderCarDAO.getOrderList(openId, orderState);
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 * @return
	 */
	public ResponseOrderDetails getOrderDetails(Long orderId){
		ResponseOrderDetails responseOrderDetails=new ResponseOrderDetails();
		
		BusOrderCarDTO busOrderCarDTO=busOrderCarDAO.getOrderDetails(orderId);
		if(Objects.isNull(busOrderCarDTO)){
			return responseOrderDetails;
		}
		List<BusCarRide> BusCarRidelist=busCarRideDAO.selectBusCarRideByOrderId(orderId);
		List<CompanionDTO> companionlist=new ArrayList<CompanionDTO>();
		for(BusCarRide busCarRide:BusCarRidelist){
			CompanionDTO companionDTO=new CompanionDTO();
			companionDTO.setCompanionName(busCarRide.getRideName());
			companionDTO.setCompanionPhone(busCarRide.getRidePhone());
			companionlist.add(companionDTO);
			
		}
		responseOrderDetails.setCompanionlist(companionlist);;
		BeanUtil.copyProperties(busOrderCarDTO, responseOrderDetails);
		if(Objects.nonNull(busOrderCarDTO.getAllocationId())){
			CarMessageDTO carMessageDTO=busCarAllocationDAO.selectAdmCarMessage(busOrderCarDTO.getAllocationId());
			responseOrderDetails.setCarMessageDTO(carMessageDTO);
		}
		return responseOrderDetails;
	}
}
